import { reactive } from 'vue'
// 限制表单数据格式
interface LoginForms {
    username: string,
    email: string;
    password: string;
}

// 限制表单校验规则格式
interface LoginRules {
    username: (
        | {
            required: boolean;
            trigger: string;
            message: string;
            type?: undefined;
        }
        | {
            type: string;
            trigger: string;
            message: string;
            required?: undefined;
        }
    )[];

    email: (
        | {
            required: boolean;
            trigger: string;
            message: string;
            type?: undefined;
        }
        | {
            type: string;
            trigger: string;
            message: string;
            required?: undefined;
        }
    )[];
    password: (
        | {
            required: boolean;
            trigger: string;
            message: string;
            min?: undefined;
            max?: undefined;
        }
        | {
            min: number;
            max: number;
            trigger: string;
            message: string;
            required?: undefined;
        }
    )[];

}

// 表单信息
export var loginForm = reactive<LoginForms>({
    username: "",
    email: "",
    password: "",
});

// 表单校验规则
export const loginRules = reactive<LoginRules>({

    username: [
        {
            required: true,
            trigger: "blur",
            message: "账号必填！"
        },
        {
            type: "string",
            trigger: "blur",
            message: "请填写账号！"
        }
    ],

    email: [
        {
            required: true,
            trigger: "blur",
            message: "邮箱必填！"
        },
        {
            type: "email",
            trigger: "blur",
            message: "请填写正确的邮箱格式！"
        }
    ],
    password: [
        {
            required: true,
            trigger: "blur",
            message: "密码必填！"
        },
        {
            min: 6,
            max: 20,
            trigger: "blur",
            message: "密码长度在6～20之间"
        }
    ],

});

// 清除表单信息
export const resetLoginForm = () => {
    console.log('reset')
    loginForm = {
        username: '',
        email: "",
        password: "",
    }
}